package com.example.springboottestservice.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

@Service
public class TestService {

    @Async
    public String test(){
        System.out.println("执行线程名称"+new Date() +Thread.currentThread().getName());
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行线程名称完成"+new Date()+Thread.currentThread().getName());
        return "success";
    }


    @Async
    public CompletableFuture<String> test2(){
        System.out.println("执行线程名称"+new Date() +Thread.currentThread().getName());
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行线程名称完成"+new Date()+Thread.currentThread().getName());
        return CompletableFuture.completedFuture("success");
    }

    @Async
    public CompletableFuture<Integer> test3(){
        System.out.println("执行线程名称"+new Date() +Thread.currentThread().getName());
        int result=0;
        try {
            for(int i=0;i<10;i++){
                result+=i;
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行线程名称完成"+new Date()+Thread.currentThread().getName());
        return CompletableFuture.completedFuture(result);
    }

    @Async
    public CompletableFuture<Integer> test4(){
        System.out.println("执行线程名称"+new Date() +Thread.currentThread().getName());
        int result=0;
        try {
            for(int i=10;i<200;i++){
                result+=i;
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行线程名称完成"+new Date()+Thread.currentThread().getName());
        return CompletableFuture.completedFuture(result);
    }

}
